<?php
Prado::using('Application.common.common');
class OrderDetails extends TPage
{
	private $_order;
	private $_orderItems;
	private $_hash;
	
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"] . " | Order Details";
		$this->setHash($this->Request['hash']);
		$this->setOrder();
		
		if ($this->Order)
		{
			$this->setOrderItems();
			
			$userRecord = UserRecord::finder()->findByPk($this->Application->User->ID);						
			$this->lblMemberDiscount->Text = "-" . Common::getFormattedValue($this->Order->CouponAmount);				
			if ($userRecord->UserType != 2 && $userRecord->UserType != 3) // not member
				$this->lblMemberType->Text = "You are not a GU member";
		}
		
		if (!$this->IsPostBack)
		{
			$this->populateCartDetails();
		}
	}
	
	public function setHash($value)
	{
		if(($value = $this->Application->SecurityManager->validateData($value))!==false)
		{
			$value=unserialize($value);
			if(is_array($value) && count($value)===3)
			{
				list($oid,$onum,$pid)=$value;
				$oid = TPropertyValue::ensureInteger($this->Application->SecurityManager->validateData(base64_decode($oid)));
				$onum = $this->Application->SecurityManager->validateData(base64_decode($onum));
				$pid = TPropertyValue::ensureInteger($this->Application->SecurityManager->validateData(base64_decode($pid)));
				$this->_hash = array('oid'=>$oid,'onum'=>$onum,'pid'=>$pid);
			}
		}
	}
	
	public function getHash()
	{
		return $this->_hash;
	}
	
	public function setOrder()
	{
		if (isset($this->Hash["oid"]) && isset($this->Hash["onum"]))
		{
			$activeRecord = OrderRecord::finder()->findByorder_idAndorder_numAnduser_id(TPropertyValue::ensureInteger($this->Hash['oid']), $this->Hash['onum'],$this->Application->User->ID);
			if($activeRecord === null)
			{
				$this->Notice->Type = UserNoticeType::Error;
				$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_NOT_FOUND","order");
				$this->mainBox->Visible = false;
			}
			$this->_order = $activeRecord;
		}
		else
		{
			$this->Notice->Type = UserNoticeType::Error;
			$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_NOT_FOUND","order");
			$this->mainBox->Visible = false;
		}
	}
	
	public function getOrder()
	{
		if (!$this->_order) $this->setOrder();
		return $this->_order;
	}
	
	public function setOrderItems()
	{
		$sqlmap = $this->Application->Modules['sqlmap']->Client;
		$this->_orderItems = $sqlmap->queryForList("GetOrderItemByOrderID", $this->Order->ID);
	}
	
	public function getOrderItems()
	{
		if (!$this->_orderItems) $this->setOrderItems();
		return $this->_orderItems;
	}
	
	public function populateCartDetails()
	{
		$this->rptCart->DataSource = $this->OrderItems;
		$this->rptCart->DataBind();
	}
	
	protected function btnContinue_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("Index"));
	}
	
	protected function btnCancel_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("account.orders.OrderHistory"));
	}
}

?>